Imports System.Data.OleDb
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmReportViewer
    Inherits DevComponents.DotNetBar.Metro.MetroForm

    Private strSelectionFormula As String = ""
    Private Report As CrystalDecisions.CrystalReports.Engine.ReportDocument
    Private cnnDB As New OleDb.OleDbConnection
    Friend WithEvents cptView As CrystalDecisions.Windows.Forms.CrystalReportViewer
    Private tbParameters As DataTable


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        cnnDB.ConnectionString = objConnectionDAO._connection.ConnectionString
        'Add any initialization after the InitializeComponent() call
        tbParameters = New DataTable
        tbParameters.Columns.Add("Name")
        tbParameters.Columns.Add("Value")
    End Sub



    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmReportViewer))
        Me.cptView = New CrystalDecisions.Windows.Forms.CrystalReportViewer()
        Me.SuspendLayout()
        '
        'cptView
        '
        Me.cptView.ActiveViewIndex = -1
        Me.cptView.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.cptView.Cursor = System.Windows.Forms.Cursors.Default
        Me.cptView.Dock = System.Windows.Forms.DockStyle.Fill
        Me.cptView.Location = New System.Drawing.Point(0, 0)
        Me.cptView.Name = "cptView"
        Me.cptView.SelectionFormula = ""
        Me.cptView.Size = New System.Drawing.Size(635, 466)
        Me.cptView.TabIndex = 0
        Me.cptView.ViewTimeSelectionFormula = ""
        '
        'frmReportViewer
        '
        Me.ClientSize = New System.Drawing.Size(635, 466)
        Me.Controls.Add(Me.cptView)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmReportViewer"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Public Property SelectionFormula() As String
        Get
            SelectionFormula = strSelectionFormula
        End Get
        Set(ByVal Value As String)
            strSelectionFormula = Value
        End Set
    End Property
    Public Property Parameters() As DataTable
        Get
            Parameters = tbParameters
        End Get
        Set(ByVal Value As DataTable)
            tbParameters = Value
        End Set
    End Property

    Public Sub PrintReport()
        Try
            Report.PrintToPrinter(1, False, 1, 999999999)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmReportViewer_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Escape
                    Dim result As Microsoft.VisualBasic.MsgBoxResult
                    result = MsgBox("Do you want to close " & Me.Text & "?", MsgBoxStyle.YesNo, "Close")
                    If result = MsgBoxResult.Yes Then
                        Me.Close()
                    End If
            End Select
        Catch ex As Exception

        End Try
    End Sub
    Public Sub ConfigReport(ByVal reportname As String, ByVal bolPrint As Boolean)
        Report = New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo
        Dim subLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo
        Dim tbInfo As CrystalDecisions.CrystalReports.Engine.Table
        Dim n As Integer

        Try
            Report.Load(Application.StartupPath & "\Reports\" & reportname)
            logonInfo.ConnectionInfo.ServerName = objConnectionDAO.connectdto.ServerName
            logonInfo.ConnectionInfo.DatabaseName = objConnectionDAO.connectdto.DatabaseName
            logonInfo.ConnectionInfo.UserID = objConnectionDAO.connectdto.UserDatabase
            If Not IsNothing(objConnectionDAO.connectdto.PasswordDatabase) Then
                logonInfo.ConnectionInfo.Password = objConnectionDAO.connectdto.PasswordDatabase
            Else
                logonInfo.ConnectionInfo.Password = ""
            End If


            'Apply logon info to the report database
            For Each tbInfo In Report.Database.Tables
                tbInfo.ApplyLogOnInfo(logonInfo)
                If tbInfo.Location <> "Command" Then
                    tbInfo.Location = tbInfo.Name
                End If
            Next

            'Apply the same logon info to sub reports,if applicable
            For n = 0 To Report.ReportDefinition.ReportObjects.Count - 1
                If Report.ReportDefinition.ReportObjects.Item(n).Kind = ReportObjectKind.SubreportObject Then
                    subLogOnInfo = Report.OpenSubreport(Report.ReportDefinition.ReportObjects.Item(n).Name).Database.Tables(0).LogOnInfo
                    subLogOnInfo.ConnectionInfo.ServerName = objConnectionDAO.connectdto.ServerName
                    subLogOnInfo.ConnectionInfo.DatabaseName = objConnectionDAO.connectdto.DatabaseName
                    subLogOnInfo.ConnectionInfo.UserID = objConnectionDAO.connectdto.UserDatabase
                    subLogOnInfo.ConnectionInfo.Password = objConnectionDAO.connectdto.PasswordDatabase
                    For Each tbInfo In Report.OpenSubreport(n).Database.Tables
                        tbInfo.ApplyLogOnInfo(subLogOnInfo)
                        tbInfo.Location = tbInfo.Name
                    Next
                End If
            Next
            If strSelectionFormula <> "" Then
                Report.RecordSelectionFormula = strSelectionFormula '"{INV_WorkOrder.WorkOrderID} = " & strSelectionFormula
            End If
            ConfigParameters()
            If bolPrint = True Then
                PrintReport()
            Else
                Me.cptView.ReportSource = Report
                cptView.Show()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub ConfigReportwithSubReport(ByVal reportname As String, ByVal bolPrint As Boolean)
        Report = New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo
        'Dim subLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo
        Dim tbInfo As CrystalDecisions.CrystalReports.Engine.Table
        Dim i, n As Integer
        Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
        Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Try
            Report.Load(Application.StartupPath & "\Reports\" & reportname)
            logonInfo.ConnectionInfo.ServerName = objConnectionDAO.connectdto.ServerName
            logonInfo.ConnectionInfo.DatabaseName = objConnectionDAO.connectdto.DatabaseName
            logonInfo.ConnectionInfo.UserID = objConnectionDAO.connectdto.UserDatabase
            If Not IsNothing(objConnectionDAO.connectdto.PasswordDatabase) Then
                logonInfo.ConnectionInfo.Password = objConnectionDAO.connectdto.PasswordDatabase
            Else
                logonInfo.ConnectionInfo.Password = ""
            End If


            'Apply logon info to the report database
            For Each tbInfo In Report.Database.Tables
                tbInfo.ApplyLogOnInfo(logonInfo)
                tbInfo.Location = tbInfo.Location.Substring(tbInfo.Location.LastIndexOf(".") + 1)
            Next

            'Apply the same logon info to sub reports,if applicable
            For i = 0 To Report.ReportDefinition.Sections.Count - 1
                For n = 0 To Report.ReportDefinition.Sections(i).ReportObjects.Count - 1
                    With Report.ReportDefinition.Sections(i)
                        If .ReportObjects(n).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                            mySubReportObject = CType(.ReportObjects(n), CrystalDecisions.CrystalReports.Engine.SubreportObject)
                            mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)

                            For Each tbInfo In mySubRepDoc.Database.Tables
                                tbInfo.ApplyLogOnInfo(logonInfo)
                                tbInfo.Location = tbInfo.Location.Substring(tbInfo.Location.LastIndexOf(".") + 1)
                            Next
                            If strSelectionFormula <> "" Then
                                mySubRepDoc.RecordSelectionFormula = strSelectionFormula
                            End If
                        End If
                    End With
                Next
            Next

            Dim paramField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
            Dim currentValues As CrystalDecisions.Shared.ParameterValues
            Dim discreteParam As New CrystalDecisions.Shared.ParameterDiscreteValue

            paramField = Report.DataDefinition.ParameterFields.Item("UnitOfMoney")
            currentValues = paramField.CurrentValues
            discreteParam.Value = True
            discreteParam.Kind = DiscreteOrRangeKind.DiscreteValue
            currentValues.Add(discreteParam)
            paramField.ApplyCurrentValues(currentValues)
            If bolPrint = True Then
                PrintReport()
            Else
                Me.cptView.ReportSource = Report
                cptView.Show()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub ConfigParameters()
        Dim paramField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
        Dim currentValues As New CrystalDecisions.Shared.ParameterValues
        Dim discreteParam As New CrystalDecisions.Shared.ParameterDiscreteValue
        Dim row As DataRow
        Try
            paramField = Report.DataDefinition.ParameterFields.Item("Username")
            currentValues = paramField.CurrentValues
            discreteParam.Value = objUser.UserFullName
            discreteParam.Kind = DiscreteOrRangeKind.DiscreteValue
            currentValues.Add(discreteParam)
            paramField.ApplyCurrentValues(currentValues)

            paramField = Report.DataDefinition.ParameterFields.Item("UnitOfMoney")
            currentValues = paramField.CurrentValues
            discreteParam.Value = True
            discreteParam.Kind = DiscreteOrRangeKind.DiscreteValue
            currentValues.Add(discreteParam)
            paramField.ApplyCurrentValues(currentValues)

            For Each row In tbParameters.Rows
                paramField = Report.DataDefinition.ParameterFields.Item(row("Name"))
                currentValues = paramField.CurrentValues
                discreteParam.Value = row("Value")
                discreteParam.Kind = DiscreteOrRangeKind.DiscreteValue
                currentValues.Add(discreteParam)
                paramField.ApplyCurrentValues(currentValues)
            Next

        Catch ex As Exception

        End Try
    End Sub

    Private Sub cptView_Load(sender As Object, e As EventArgs) Handles cptView.Load

    End Sub
End Class
